import { createRouter, createWebHistory } from 'vue-router'
import CaseLibrary from '../views/CaseLibrary.vue'
// import Workspace from '../views/Workspace.vue'

const routes = [
  {
    path: '/',
    name: 'RootRedirect',
    // 根据是否存在工作区进行动态重定向
    redirect: () => {
      try {
        const stored = localStorage.getItem('workspaces')
        const workspaces = stored ? JSON.parse(stored) : []
        if (Array.isArray(workspaces) && workspaces.length > 0) {
          return '/workspace/viewer'
        }
      } catch (_) {
        // ignore JSON errors and fall back to workspace creation
      }
      return '/workspace'
    }
  },
  {
    path: '/cases',
    name: 'CaseLibrary',
    component: CaseLibrary
  },
  {
    path: '/workspace',
    name: 'Workspace',
    component: () => import('../views/Workspace.vue')
  },
  
  {
    path: '/workspace/viewer',
    name: 'WorkspaceViewer',
    component: () => import('../views/LogViewer.vue')
  },
  {
    path: '/workspace/dump',
    name: 'WorkspaceDump',
    component: () => import('../views/DumpManagement.vue')
  },
  {
    path: '/workspace/analysis',
    name: 'WorkspaceAnalysis',
    component: () => import('../views/SmartAnalysis.vue')
  },
  {
    path: '/workspace/visualization',
    name: 'WorkspaceVisualization',
    component: () => import('../views/Visualization.vue')
  },
  {
    path: '/workspace/reports',
    name: 'WorkspaceReports',
    component: () => import('../views/Report.vue')
  },
  {
    path: '/workspace/rpc-analysis',
    name: 'WorkspaceRPCAnalysis',
    component: () => import('../views/RPCAnalysis.vue')
  },
  {
    path: '/debug',
    name: 'DebugInfo',
    component: () => import('../views/DebugInfo.vue')
  },
  {
    path: '/test-drag',
    name: 'TestDrag',
    component: () => import('../views/TestDrag.vue')
  },
  {
    path: '/debug-analysis',
    name: 'DebugAnalysis',
    component: () => import('../views/DebugAnalysis.vue')
  },

]

const router = createRouter({
  history: createWebHistory(),
  routes
})

export default router